package com.jajahome.feature.user.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jajahome.R;
import com.jajahome.base.BaseRecyclerAdapter;
import com.jajahome.base.BaseRecyclerViewHolder;
import com.jajahome.model.CrowDetailModel;
import com.jajahome.util.StringUtil;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by tuxuesong on 2018/5/23.
 */

public class CrowImgAdapter extends BaseRecyclerAdapter<CrowDetailModel.DataEntity.CrowdFundingEntity> {

    private OnImgItemClickListener listener;
    private int mIndex;

    public void setListener(OnImgItemClickListener listener) {
        this.listener = listener;
    }


    @Override
    public void showData(BaseRecyclerViewHolder viewHolder, final int i, List<CrowDetailModel.DataEntity.CrowdFundingEntity> mItemDataList) {
        ViewHolder holder = (ViewHolder) viewHolder;
        String url = mItemDataList.get(i).getImage().getSmall();
        if (!StringUtil.isEmpty(url)) {
            Picasso.with(mContext).load(url).into(holder.itemBannerImg);
        }

        if (mIndex == i) {
            holder.frameLayout.setBackgroundResource(R.drawable.select_btn_gray);
            holder.frameLayout.getBackground().setAlpha(15);
        } else {
            holder.frameLayout.setBackgroundResource(R.drawable.select_btn_white);
            holder.frameLayout.getBackground().setAlpha(15);
        }


        holder.frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(i);
                }
            }
        });
    }

    public void setmIndex(int position) {
        mIndex = position;
        notifyDataSetChanged();
    }

    @Override
    public int getListLayoutId() {
        return R.layout.item_preview_int_item;
    }

    @Override
    public BaseRecyclerViewHolder createViewHolder(View view) {
        return new ViewHolder(view);
    }

    class ViewHolder extends BaseRecyclerViewHolder {
        ImageView itemBannerImg;
        LinearLayout frameLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            frameLayout = (LinearLayout) findView(R.id.frame_layout);
            itemBannerImg = (ImageView) findView(R.id.item_img);
        }
    }

    public interface OnImgItemClickListener {
        void onItemClick(int index);
    }

}
